<?php

class DateTimeDutch extends DateTime {
    public function format($format) {
        $en = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday','January','February','March','April','May','June','July','August','September','October','November','December');
        $nl = array('Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag', 'Zondag','Januari','Februari','Maart','April','Mei','June','Juli','Augustus','September','Oktober','November','December');
        return str_replace($en, $nl, parent::format($format));
    }
    public function addMinutes($min) {
         $test1 = strtotime(parent::format('Y-m-d H:i:s'));
         $test2 = strtotime("+".$min."Min", $test1);
         //parent::createFromFormat("Y-m-d H:i:s",date("Y-m-d H:i:s", $test2));
         //parent::setTimezone($timezone);        
    }
}

class Point
{
    public $lat;
    public $lng;
    public $plaats;
    public $gemeente;
    public $provincie;

    function __construct($lat,$lng)
    {
        $this->lat = $lat;
        $this->lng = $lng;
    } 
}

class PointHandler
{    
    public function getPoint($address) {    
      $handle = fopen("http://maps.googleapis.com/maps/api/geocode/json?language=nl&address=$address,+Nederland&sensor=false", "r");
      $response='';
      while(!feof($handle))
          $response.=fread($handle,1024);
      fclose($handle); 

      $responseStriped = $this->stripHtml($response);
      $json = json_decode($responseStriped);
      
      
      if (!is_object($json) || !isset($json->results[0]) || !is_object($json->results[0])) {
        //throw new Exception("No point found for address:$address:json:$json");
        throw new Exception("No point found for address");
        return null;
      }
      
      if (isset($json->results[0]) && ($json->results[0]->types[0] == "postal_code")) { 
        $result = $json->results[0];  
      } else if (isset($json->results[1]) && ($json->results[1]->types[0] == "postal_code")) {
        $result = $json->results[1];  
      } else if (isset($json->results[2]) && ($json->results[2]->types[0] == "postal_code")) {
        $result = $json->results[2];  
      } else {
        $result = $json->results[0];
      }
      
      $lat = $json->results[0]->geometry->location->{'lat'};
      $lng = $json->results[0]->geometry->location->{'lng'};
      
      $point = new Point($lat,$lng);
      
      $arr = $json->results[0]->address_components;
      
      foreach ($arr as &$value) {
        if ( (isset($value->types[0]) && $value->types[0] == "locality" ) ||
             (isset($value->types[1]) && $value->types[1] == "locality" ) ) {
           $point->plaats = $value->long_name;
        }        
        if ( (isset($value->types[0]) && $value->types[0] == "administrative_area_level_2" ) ||
             (isset($value->types[1]) && $value->types[1] == "administrative_area_level_2" ) ) {
           $point->gemeente = $value->long_name;
        }
        if ( (isset($value->types[0]) && $value->types[0] == "administrative_area_level_1" ) ||
             (isset($value->types[1]) && $value->types[1] == "administrative_area_level_1" ) ) {
           $point->provincie = $value->long_name;
        }
      }
                    
      return $point;
    }

    public function distance($point1, $point2) {
      $d = $this->haversineFormula($point1->lat,$point1->lng,$point2->lat,$point2->lng);
      
      return $d;
    }
    
    private function haversineFormula($latitude1, $longitude1, $latitude2, $longitude2) {  
      $earth_radius = 6371;  
       
      $dLat = deg2rad($latitude2 - $latitude1);  
      $dLon = deg2rad($longitude2 - $longitude1);  
       
      $a = sin($dLat/2) * sin($dLat/2) + cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * sin($dLon/2) * sin($dLon/2);  
      $c = 2 * asin(sqrt($a));  
      $d = $earth_radius * $c;  
       
      return $d;  
    }
    
    private function stripHtml($content) {
      $rtn = $content;
      $replace = array("","","");
      $search1a = array("<html>","<body>","<head>");
      $search1b = array("</html>","</body>","</head>");
      $rtn = str_replace($search1a,$replace,$rtn);
      $rtn = str_replace($search1a,$replace,$rtn);
      return $rtn;
    }
    
}
